﻿using AntMovement.Core;
using AntMovement.Core.IParameter;
using AntMovement.Core.Model;
using AntMovement.Core.Mvvm;
using AntMovement.Core.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace AntMovement.Parameter
{
    public class HoldParameter : ValueChangeBase, IHoldParameter
    {
        #region 私有
        private Action<HoldParameter, HoldParameter> _copy;
        #endregion

        public HoldParameter() : base(null)
        {
            
        }
        public HoldParameter(ILog log) : base(log)
        {
            _copy = Core.Tool.ConversionHelper.CopyClass<HoldParameter, HoldParameter>();
            if (System.IO.File.Exists(_path))
            {
                string str = System.IO.File.ReadAllText(_path, Encoding.UTF8);
                if (!string.IsNullOrEmpty(str))
                {
                    try
                    {
                        var hold = System.Text.Json.JsonSerializer.Deserialize<HoldParameter>(str, OptionsHelper.Options);
                        hold.CurrentStackingLine = 0;
                        hold.CurrentStackingColumn = 0;
                        hold.CurrentDiskPosition = 0;
                        _copy.Invoke(hold, this);
                    }
                    catch (Exception ex)
                    {
                        _log.Error("异常",$"HoldParameter => {ex}");
                    }
                }
            }
        }

        public void Save()
        {
            if (IsChange)
            {
                Locked();
                System.IO.File.WriteAllText(_path, System.Text.Json.JsonSerializer.Serialize(this, OptionsHelper.Options), Encoding.UTF8);
                IsChange = false;
                UnLock();
                //_log.Warning("保持变量保存成功...");
            }
        }

        #region 属性

        private double _MaterialSpacingLine;
        /// <summary>
        /// 物料行间隔
        /// </summary>
        public double MaterialSpacingLine
        {
            get { return _MaterialSpacingLine; }
            set { SetProperty(ref _MaterialSpacingLine, value); }
        }

        private double _MaterialSpacingColumn;
        /// <summary>
        /// 物料列间隔
        /// </summary>
        public double MaterialSpacingColumn
        {
            get { return _MaterialSpacingColumn; }
            set { SetProperty(ref _MaterialSpacingColumn, value); }
        }

        private int _CurrentStackingLine = 0;
        /// <summary>
        /// 当前行位置
        /// </summary>
        public int CurrentStackingLine
        {
            get { return _CurrentStackingLine; }
            set { SetProperty(ref _CurrentStackingLine, value); }
        }

        private int _CurrentStackingColumn = 0;
        /// <summary>
        /// 当前列位置
        /// </summary>
        public int CurrentStackingColumn
        {
            get { return _CurrentStackingColumn; }
            set { SetProperty(ref _CurrentStackingColumn, value); }
        }

        private int _CurrentFloors;
        /// <summary>
        /// 当前层数
        /// </summary>
        public int CurrentFloors
        {
            get { return _CurrentFloors; }
            set { SetProperty(ref _CurrentFloors, value); }
        }

        private int _CurrentDiskPosition;
        /// <summary>
        /// 当前盘位
        /// </summary>
        public int CurrentDiskPosition
        {
            get { return _CurrentDiskPosition; }
            set { SetProperty(ref _CurrentDiskPosition, value); }
        }

        private int _YCount;

        public int YCount
        {
            get { return _YCount; }
            set { SetValue(ref _YCount, value); }
        }

        private int _XCount;

        public int XCount
        {
            get { return _XCount; }
            set { SetValue(ref _XCount, value); }
        }

        private int _r1Compensate;

        public int R1Compensate
        {
            get { return _r1Compensate; }
            set { SetValue(ref _r1Compensate, value); }
        }

        private int _r2Compensate;

        public int R2Compensate
        {
            get { return _r2Compensate; }
            set { SetValue(ref _r2Compensate, value); }
        }

        private int _r3Compensate;

        public int R3Compensate
        {
            get { return _r3Compensate; }
            set { SetValue(ref _r3Compensate, value); }
        }
        #endregion

        private int _DirectShockTime = 3000;
        /// <summary>
        /// 直震时间
        /// </summary>
        public int DirectShockTime
        {
            get { return _DirectShockTime; }
            set { SetValue(ref _DirectShockTime, value); }
        }

        #region Z轴高度
        private double _ZAxisWithdrawOne = 35;
        /// <summary>
        /// Z轴1#获取高度
        /// </summary>
        public double ZAxisWithdrawOne
        {
            get { return _ZAxisWithdrawOne; }
            set { SetValue(ref _ZAxisWithdrawOne, value); }
        }

        private double _ZAxisWithdrawTwo = 37;
        /// <summary>
        /// Z轴2#获取高度
        /// </summary>
        public double ZAxisWithdrawTwo
        {
            get { return _ZAxisWithdrawTwo; }
            set { SetValue(ref _ZAxisWithdrawTwo, value); }
        }

        private double _ZAxisWithdrawThree = 36;
        /// <summary>
        /// Z轴3#获取高度
        /// </summary>
        public double ZAxisWithdrawThree
        {
            get { return _ZAxisWithdrawThree; }
            set { SetValue(ref _ZAxisWithdrawThree, value); }
        }

        private double _ZAxisPlaceOne = 38.5;
        /// <summary>
        /// Z轴1#放置高度
        /// </summary>
        public double ZAxisPlaceOne
        {
            get { return _ZAxisPlaceOne; }
            set { SetValue(ref _ZAxisPlaceOne, value); }
        }

        private double _ZAxisPlaceTwo = 40;
        /// <summary>
        /// Z轴2#放置高度
        /// </summary>
        public double ZAxisPlaceTwo
        {
            get { return _ZAxisPlaceTwo; }
            set { SetValue(ref _ZAxisPlaceTwo, value); }
        }

        private double _ZAxisPlaceThree = 41;
        /// <summary>
        /// Z轴3#放置高度
        /// </summary>
        public double ZAxisPlaceThree
        {
            get { return _ZAxisPlaceThree; }
            set { SetValue(ref _ZAxisPlaceThree, value); }
        }
        #endregion
    }
}
